﻿Public Class Form1

    ' When you declare your variables make sure to initialize them.
    Dim objApp As SolidEdgeFramework.Application = Nothing
    Dim bStartSolidEdge As Boolean = False
    Dim bFailure As Boolean = False
    Dim sMsg As String = Nothing
    Dim sGlobalControlText_Default As String = "Default - Use text and no text buttons"
    Dim sGlobalControlText_NoText As String = "Do not use any text on buttons"
    Dim sGlobalControlText_UseText As String = "Use text on all buttons"
    Dim sGlobalControlSize_Default As String = "Default - Use small and large buttons"
    Dim sGlobalControlSize_Large As String = "Use only large buttons"
    Dim sGlobalControlSize_Small As String = "Use only small buttons"
    Dim bCustomize As Boolean = False

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        ' This is done to allow calls into SE to be rerun if SE wasn't able to the first time.
        MyMessageFilter.MessageFilter.Register()

        ' Connect to Solid Edge if its up if not then start Solid Edge
        StartSolidEdge(bFailure, sMsg)
        If (bFailure) Then
            MsgBox(sMsg)
            GoTo NoSolidEdge
        End If

        LoadThemes("Solid Edge Default")  ' Put the Themes in the Combo box.

NoSolidEdge:

    End Sub
    ' Either connect to a running Solid Edge or start Solid Edge.
    Public Sub StartSolidEdge(ByRef bFailure As Boolean, ByRef sMsg As String)
        objApp = Nothing

        bFailure = False

TryCreate:

        Try
            If (bStartSolidEdge) Then
                objApp = CreateObject("SolidEdge.Application")
            Else
                objApp = GetObject(, "Solidedge.Application")
            End If
        Catch ex As Exception
            If (bStartSolidEdge = True) Then
                bFailure = True
                sMsg = ex.Message
                GoTo ErrorStartingSE
            End If
            bStartSolidEdge = True
            GoTo TryCreate
        End Try


        Try
            objApp.Visible = True  ' Make Solid Edge Visible
        Catch ex As Exception
            sMsg = ex.Message

        End Try

        Try
            objApp.DisplayAlerts = False  ' Don't display error messages
        Catch ex As Exception
        End Try

ErrorStartingSE:
    End Sub

    Private Sub Custom_Themes_ComboBox_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles Custom_Themes_ComboBox.SelectedIndexChanged
        Dim nCustomeTheme_ComboIndex As Integer = 0
        Dim objRibbonBarTheme As SolidEdgeFramework.RibbonBarTheme = Nothing

        Dim sGlobalControlSize As String = Nothing
        Dim sGlobalControlText As String = Nothing

        Windows.Forms.Cursor.Current = Cursors.WaitCursor

        Try
            ' Put this at the beginning before you do any customization 
            objApp.Customization.BeginCustomization()

            GlobalControlSize_CBox.Enabled = True
            GlobalControlText_CBox.Enabled = True

            ' Can't remove the Soiid Edge Default theme.
            If (Custom_Themes_ComboBox.SelectedItem = "Solid Edge Default") Then
                Clear_Disable_Controls()
                objApp.Customization.RibbonBarThemes.ActivateTheme(Nothing) ' Set the Theme to Solid Edge Default
                GoTo Finished_Custom_Themes_ComboBox_Selection_Change
            Else
                RemoveTheme_Btn.Enabled = True
                GlobalControlSize_CBox.Enabled = True
                GlobalControlText_CBox.Enabled = True
                RibbonBar_Env_ComboBox.Enabled = True
            End If


            objRibbonBarTheme = GetTheme(objApp, Custom_Themes_ComboBox.SelectedItem)
            objRibbonBarTheme.Parent.ActivateTheme(objRibbonBarTheme)

            'Num_RibbonBars_Label.Text = "Number of Ribbon Bars: " + objRibbonBarTheme.RibbonBars.Count.ToString

            ' Setup the ribbon bar environments.
            RibbonBar_Env_ComboBox.Items.Clear()

            For i = 0 To objRibbonBarTheme.RibbonBars.Count - 1
                RibbonBar_Env_ComboBox.Items.Add(objRibbonBarTheme.RibbonBars.Item(i).Environment)
            Next


            ' Fill in the Global listboxes
            bCustomize = False           ' When updating these controls we call a BeginCustomization - EndCustomization this will cause flashing set flag to stop it.
            GlobalControlText_CBox.Items.Clear()
            GlobalControlText_CBox.Items.Add(sGlobalControlText_Default) '"Default - Use text and no text buttons")
            GlobalControlText_CBox.Items.Add(sGlobalControlText_NoText)
            GlobalControlText_CBox.Items.Add(sGlobalControlText_UseText)
            GlobalControlText_CBox.Enabled = True ' False   Not sure why I set this to False

            GlobalControlSize_CBox.Items.Clear()
            GlobalControlSize_CBox.Items.Add(sGlobalControlSize_Default)
            GlobalControlSize_CBox.Items.Add(sGlobalControlSize_Large)
            GlobalControlSize_CBox.Items.Add(sGlobalControlSize_Small)
            GlobalControlSize_CBox.Enabled = True ' False


            Dim CtrlSize As SolidEdgeFramework.RibbonBarControlSize

            CtrlSize = objRibbonBarTheme.GlobalControlSize
            If (SolidEdgeFramework.RibbonBarControlSize.seRibbonBarControlSizeDefault = objRibbonBarTheme.GlobalControlSize) Then
                sGlobalControlSize = sGlobalControlSize_Default
            Else
                If (SolidEdgeFramework.RibbonBarControlSize.seRibbonBarControlSizeLarge = objRibbonBarTheme.GlobalControlSize) Then
                    sGlobalControlSize = sGlobalControlSize_Large
                Else
                    If (SolidEdgeFramework.RibbonBarControlSize.seRibbonBarControlSizeSmall = objRibbonBarTheme.GlobalControlSize) Then
                        sGlobalControlSize = sGlobalControlSize_Small
                    End If
                End If
            End If

            If (SolidEdgeFramework.RibbonBarControlText.seRibbonBarControlTextDefault = objRibbonBarTheme.GlobalControlText) Then
                sGlobalControlText = sGlobalControlText_Default
            Else
                If (SolidEdgeFramework.RibbonBarControlText.seRibbonBarControlTextOff = objRibbonBarTheme.GlobalControlText) Then
                    sGlobalControlText = sGlobalControlText_NoText
                Else
                    If (SolidEdgeFramework.RibbonBarControlText.seRibbonBarControlTextOn = objRibbonBarTheme.GlobalControlText) Then
                        sGlobalControlText = sGlobalControlText_UseText
                    End If
                End If
            End If


Finished_Custom_Themes_ComboBox_Selection_Change:

            objApp.Customization.RibbonBarThemes.Commit()
            objApp.Customization.EndCustomization()

            GlobalControlSize_CBox.SelectedItem = sGlobalControlSize
            GlobalControlText_CBox.SelectedItem = sGlobalControlText

            bCustomize = True   ' Reset the flag so changing values will work sent set by the controls.

        Catch ex As Exception

        End Try

        releaseObject(objRibbonBarTheme)

    End Sub
    Private Sub RibbonBar_Env_ComboBox_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles RibbonBar_Env_ComboBox.SelectedIndexChanged
        Update_Tree_Based_on_Selected_Env()
    End Sub
    Private Sub ChangeVisibleState(ByVal sItem As String)
        Dim InfoArray() As String = Nothing
        InfoArray = Nothing

        Windows.Forms.Cursor.Current = Cursors.WaitCursor

        Dim objRibbonBarTheme As SolidEdgeFramework.RibbonBarTheme = Nothing
        Dim objRibbonBar As SolidEdgeFramework.RibbonBar = Nothing
        Dim objTab As SolidEdgeFramework.RibbonBarTab = Nothing
        Dim objGroup As SolidEdgeFramework.RibbonBarGroup = Nothing
        Dim objControl As SolidEdgeFramework.RibbonBarControl = Nothing

        InfoArray = Split(sItem, "\")


        ' Put this at the beginning.  
        objApp.Customization.BeginCustomization()

        objRibbonBarTheme = GetTheme(objApp, Custom_Themes_ComboBox.SelectedItem)

        objRibbonBar = GetRibbonbar(objRibbonBarTheme)

        If (InfoArray.Count = 1) Then
            objTab = GetRibbonTab(objRibbonBar, sItem)
            If (objTab IsNot Nothing) Then
                objTab.Visible = Not (objTab.Visible)
            End If
        ElseIf (InfoArray.Count = 2) Then
            objGroup = GetRibbonGroup(objRibbonBar, sItem)
            If (objGroup IsNot Nothing) Then
                objGroup.Visible = Not (objGroup.Visible)
            End If
        Else
            objControl = GetRibbonControl(objRibbonBar, sItem)
            If (objControl IsNot Nothing) Then
                objControl.Visible = Not (objControl.Visible)
            End If
        End If

        objApp.Customization.RibbonBarThemes.Commit()
        objApp.Customization.EndCustomization()


        releaseObject(objRibbonBarTheme)
        releaseObject(objRibbonBar)
        releaseObject(objTab)
        releaseObject(objGroup)
        releaseObject(objControl)

    End Sub
    Private Sub TreeView1_AfterCheck(sender As System.Object, e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterCheck

        Windows.Forms.Cursor.Current = Cursors.WaitCursor

        If e.Action <> TreeViewAction.Unknown Then
            ChangeVisibleState(e.Node.FullPath)
        End If

    End Sub

    Private Sub TreeView1_AfterSelect(sender As System.Object, e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        Dim objRibbonBarTheme As SolidEdgeFramework.RibbonBarTheme = Nothing
        Dim objRibbonBar As SolidEdgeFramework.RibbonBar = Nothing
        Dim objControl As SolidEdgeFramework.RibbonBarControl = Nothing

        Dim objTab As SolidEdgeFramework.RibbonBarTab = Nothing
        Dim InfoArray() As String = Nothing
        InfoArray = Nothing
        Dim i As Integer = 0
        Dim sControlSize As String = Nothing
        Dim sControlText As String = Nothing


        If e.Action <> TreeViewAction.Unknown Then

            objRibbonBarTheme = GetTheme(objApp, Custom_Themes_ComboBox.SelectedItem)

            objRibbonBar = GetRibbonbar(objRibbonBarTheme)

            objTab = GetRibbonTab(objRibbonBar, Nothing)

            InfoArray = Split(e.Node.FullPath, "\")

            Insert_Btn.Enabled = False
            Remove_Btn.Enabled = False
            Insert_Group_Btn.Enabled = False
            Remove_Group_Btn.Enabled = False
            Insert_Control_Btn.Enabled = False
            Remove_Control_Btn.Enabled = False

            Control_Size.Visible = False
            ControlSize_CBox.Visible = False
            Control_Text.Visible = False
            ControlText_CBox.Visible = False
            ControlID_TB.Visible = False
            StartCommand_BTN.Visible = False
            ControlName_LB.Visible = False

            If (InfoArray.Count = 1) Then
                ' If they have selected an environment then 
                Insert_Btn.Enabled = True
                Remove_Btn.Enabled = True

                Insert_Group_Btn.Enabled = True ' You can insert a group


            ElseIf (InfoArray.Count = 2) Then
                Insert_Group_Btn.Enabled = True
                Remove_Group_Btn.Enabled = True

                Insert_Control_Btn.Enabled = True ' You can insert a control
            Else
                Insert_Control_Btn.Enabled = True
                Remove_Control_Btn.Enabled = True

                Control_Size.Visible = True
                ControlSize_CBox.Visible = True
                Control_Text.Visible = True
                ControlText_CBox.Visible = True
                ControlID_TB.Visible = True

                ControlText_CBox.Items.Clear()
                ControlText_CBox.Items.Add(sGlobalControlText_Default) '"Default - Use text and no text buttons")
                ControlText_CBox.Items.Add(sGlobalControlText_NoText)
                ControlText_CBox.Items.Add(sGlobalControlText_UseText)
                ControlText_CBox.Enabled = True

                ControlSize_CBox.Items.Clear()
                ControlSize_CBox.Items.Add(sGlobalControlSize_Default)
                ControlSize_CBox.Items.Add(sGlobalControlSize_Large)
                ControlSize_CBox.Items.Add(sGlobalControlSize_Small)
                ControlSize_CBox.Enabled = True
                StartCommand_BTN.Visible = True
                ControlName_LB.Visible = True

                objControl = GetRibbonControl(objRibbonBar, Nothing)

                bCustomize = False ' set this so we doesn't flash as much.

                If (objControl IsNot Nothing) Then

                    ' Control ID
                    ControlID_TB.Text = objControl.Id.ToString
                    ControlName_LB.Text = ControlName_LB.Text + objControl.Name

                    If (SolidEdgeFramework.RibbonBarControlSize.seRibbonBarControlSizeDefault = objControl.Size) Then
                        sControlSize = sGlobalControlSize_Default
                    Else
                        If (SolidEdgeFramework.RibbonBarControlSize.seRibbonBarControlSizeLarge = objControl.Size) Then
                            sControlSize = sGlobalControlSize_Large
                        Else
                            If (SolidEdgeFramework.RibbonBarControlSize.seRibbonBarControlSizeSmall = objControl.Size) Then
                                sControlSize = sGlobalControlSize_Small
                            End If
                        End If
                    End If

                    If (SolidEdgeFramework.RibbonBarControlText.seRibbonBarControlTextDefault = objControl.Text) Then
                        sControlText = sGlobalControlText_Default
                    Else
                        If (SolidEdgeFramework.RibbonBarControlText.seRibbonBarControlTextOff = objControl.Text) Then
                            sControlText = sGlobalControlText_NoText
                        Else
                            If (SolidEdgeFramework.RibbonBarControlText.seRibbonBarControlTextOn = objControl.Text) Then
                                sControlText = sGlobalControlText_UseText
                            End If
                        End If
                    End If


                    ControlSize_CBox.SelectedItem = sControlSize
                    ControlText_CBox.SelectedItem = sControlText
                    bCustomize = True

                End If

            End If

        End If

        releaseObject(objRibbonBarTheme)
        releaseObject(objRibbonBar)
        releaseObject(objTab)
        releaseObject(objControl)


    End Sub

    Private Sub Remove_Btn_Click(sender As System.Object, e As System.EventArgs) Handles Remove_Btn.Click
        Dim objRibbonBarTheme As SolidEdgeFramework.RibbonBarTheme = Nothing
        Dim objRibbonBar As SolidEdgeFramework.RibbonBar = Nothing
        Dim objTab As SolidEdgeFramework.RibbonBarTab = Nothing
        Dim objGroup As SolidEdgeFramework.RibbonBarGroup = Nothing
        Dim objControl As SolidEdgeFramework.RibbonBarControl = Nothing

        Dim InfoArray() As String = Nothing
        InfoArray = Nothing

        Try
            ' Put this at the beginning.  
            objApp.Customization.BeginCustomization()

            objRibbonBarTheme = GetTheme(objApp, Custom_Themes_ComboBox.SelectedItem)

            objRibbonBar = GetRibbonbar(objRibbonBarTheme)


            Dim objTreeNode As New TreeNode

            objTreeNode = TreeView1.SelectedNode
           
            InfoArray = Split(objTreeNode.FullPath, "\")

            If (InfoArray.Count = 1) Then
                objTab = GetRibbonTab(objRibbonBar, Nothing)
            ElseIf (InfoArray.Count = 2) Then
                objGroup = GetRibbonGroup(objRibbonBar, Nothing)
            Else
                objControl = GetRibbonControl(objRibbonBar, Nothing)
            End If

            objRibbonBar.RibbonBarTabs.Remove(objTab)
            objTab = Nothing

            objTreeNode.Remove()
            objTreeNode = Nothing

            objApp.Customization.RibbonBarThemes.Commit()
            objApp.Customization.EndCustomization()


            Insert_Btn.Enabled = False
            Remove_Btn.Enabled = False
            Insert_Group_Btn.Enabled = False
            Remove_Group_Btn.Enabled = False
            Insert_Control_Btn.Enabled = False
            Remove_Control_Btn.Enabled = False

           

            'If they removed all tabs.
            If (TreeView1.Nodes.Count = 0) Then
                Insert_Btn.Enabled = True
            End If

        Catch ex As Exception

        End Try

        releaseObject(objGroup)
        releaseObject(objControl)
        releaseObject(objTab)
        releaseObject(objRibbonBar)
        releaseObject(objRibbonBarTheme)


        ' Unselect the node.
        TreeView1.SelectedNode = Nothing

    End Sub
    Public Function GetRibbonGroups(ByVal objRibbonBar As SolidEdgeFramework.RibbonBar, ByVal sGroupLocation As String)
        Dim objGroups As SolidEdgeFramework.RibbonBarGroups = Nothing
        Dim objTreeNode As New TreeNode

        Dim i As Integer = 0

        Try
            Dim InfoArray() As String = Nothing
            InfoArray = Nothing

            If (sGroupLocation Is Nothing) Then
                objTreeNode = TreeView1.SelectedNode
                InfoArray = Split(objTreeNode.FullPath, "\")
            Else
                InfoArray = Split(sGroupLocation, "\")
            End If

            For i = 0 To objRibbonBar.RibbonBarTabs.Count - 1
                If (objRibbonBar.RibbonBarTabs.Item(i).Name = objTreeNode.Text) Then
                    objGroups = objRibbonBar.RibbonBarTabs.Item(i).RibbonBarGroups
                    Exit For
                End If
            Next

        Catch ex As Exception

        End Try

        GetRibbonGroups = objGroups

    End Function
    Public Function GetRibbonGroup(ByVal objRibbonBar As SolidEdgeFramework.RibbonBar, ByVal sGroupLocation As String)
        Dim objGroup As SolidEdgeFramework.RibbonBarGroup = Nothing
        Dim objTreeNode As New TreeNode


        Dim i As Integer = 0
        Dim j As Integer = 0

        Try
            Dim InfoArray() As String = Nothing
            InfoArray = Nothing

            If (sGroupLocation Is Nothing) Then
                objTreeNode = TreeView1.SelectedNode
                InfoArray = Split(objTreeNode.FullPath, "\")
            Else
                InfoArray = Split(sGroupLocation, "\")
            End If

            For i = 0 To objRibbonBar.RibbonBarTabs.Count - 1

                If (objRibbonBar.RibbonBarTabs.Item(i).RibbonBarGroups IsNot Nothing) Then

                    ' Make sure the tab is the correct one.  Same group is one multiple tabs.
                    If (objRibbonBar.RibbonBarTabs.Item(i).Name = InfoArray(0)) Then

                        ' Go though all of the groups and find the one with the same name.
                        For j = 0 To objRibbonBar.RibbonBarTabs.Item(i).RibbonBarGroups.Count - 1

                            ' Get the group
                            If (InfoArray(1) = objRibbonBar.RibbonBarTabs.Item(i).RibbonBarGroups.Item(j).Name) Then
                                objGroup = objRibbonBar.RibbonBarTabs.Item(i).RibbonBarGroups.Item(j)
                                Exit For
                            End If

                        Next
                    End If

                End If

                If (objGroup IsNot Nothing) Then
                    Exit For
                End If
            Next

        Catch ex As Exception

        End Try


        GetRibbonGroup = objGroup

    End Function
    Public Function GetControl(ByVal objControls As SolidEdgeFramework.RibbonBarControls, ByVal sControl As String)
        Dim i As Integer = 0
        Dim objControl As SolidEdgeFramework.RibbonBarControl = Nothing

        If (objControls IsNot Nothing) Then
            For i = 0 To objControls.Count - 1
                If (sControl = objControls.Item(i).Name) Then
                    objControl = objControls.Item(i)
                    Exit For
                End If
            Next

        End If
        GetControl = objControl


    End Function
    Public Function GetRibbonControl(ByVal objRibbonBar As SolidEdgeFramework.RibbonBar, ByVal sControlLocation As String)
        Dim objTreeNode As New TreeNode
        Dim objGroup As SolidEdgeFramework.RibbonBarGroup = Nothing
        Dim sControl As String = Nothing
        Dim objControl As SolidEdgeFramework.RibbonBarControl = Nothing
        Dim objControls As SolidEdgeFramework.RibbonBarControls = Nothing


        Dim i As Integer = 0
        Dim j As Integer = 0
        Dim k As Integer = 0

        objGroup = GetRibbonGroup(objRibbonBar, sControlLocation)

        Dim InfoArray() As String = Nothing
        InfoArray = Nothing

        If (sControlLocation Is Nothing) Then
            objTreeNode = TreeView1.SelectedNode
            InfoArray = Split(objTreeNode.FullPath, "\")
        Else
            InfoArray = Split(sControlLocation, "\")
        End If

        If (InfoArray.Count > 2) Then
            objControls = objGroup.RibbonBarControls
            For i = 2 To InfoArray.Count - 1
                sControl = InfoArray(i)
                objControl = GetControl(objControls, sControl)
                If (i < InfoArray.Count - 1) Then
                    objControls = objControl.RibbonBarControls
                End If
            Next
        End If

        GetRibbonControl = objControl

        releaseObject(objControls)
        releaseObject(objGroup)

    End Function
    Public Function GetRibbonTab(ByVal objRibbonBar As SolidEdgeFramework.RibbonBar, ByVal sTabName As String)
        Dim objTab As SolidEdgeFramework.RibbonBarTab = Nothing
        Dim objTreeNode As New TreeNode

        Dim i As Integer = 0
        Dim InfoArray() As String = Nothing
        InfoArray = Nothing

        Try
            If (sTabName Is Nothing) Then
                objTreeNode = TreeView1.SelectedNode
                If (objTreeNode Is Nothing) Then
                    GoTo NoTabSelected
                End If
                InfoArray = Split(objTreeNode.FullPath, "\")
            Else
                InfoArray = Split(sTabName, "\")
            End If


            If (objRibbonBar.RibbonBarTabs IsNot Nothing) Then
                For i = 0 To objRibbonBar.RibbonBarTabs.Count - 1
                    If (InfoArray(0) = objRibbonBar.RibbonBarTabs.Item(i).Name) Then
                        objTab = objRibbonBar.RibbonBarTabs.Item(i)
                        Exit For
                    End If
                Next

            End If

        Catch ex As Exception

        End Try

NoTabSelected:
        GetRibbonTab = objTab

    End Function
    Public Function GetRibbonbar(ByVal objRibbonBarTheme As SolidEdgeFramework.RibbonBarTheme)
        Dim objRibbonBar As SolidEdgeFramework.RibbonBar = Nothing

        For i = 0 To objRibbonBarTheme.RibbonBars.Count - 1
            If (RibbonBar_Env_ComboBox.SelectedItem = objRibbonBarTheme.RibbonBars.Item(i).Environment) Then
                objRibbonBar = objRibbonBarTheme.RibbonBars.Item(i)
                Exit For
            End If
        Next

        GetRibbonbar = objRibbonBar

    End Function
    Public Function GetTheme(ByVal objApp As SolidEdgeFramework.Application, ByVal sCustomThemeName As String) As SolidEdgeFramework.RibbonBarTheme
        Dim i As Integer = 0
        Dim objRibbonBarTheme As SolidEdgeFramework.RibbonBarTheme = Nothing

        Try
            For i = 0 To objApp.Customization.RibbonBarThemes.Count - 1
                objRibbonBarTheme = objApp.Customization.RibbonBarThemes.Item(i)
                If (objRibbonBarTheme.Name = sCustomThemeName) Then
                    Exit For
                End If

                releaseObject(objRibbonBarTheme)

            Next

        Catch ex As Exception

        End Try

        GetTheme = objRibbonBarTheme

    End Function

    Private Sub CreateTheme_Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateTheme_Btn.Click
        Dim objRibbonBarTheme As SolidEdgeFramework.RibbonBarTheme = Nothing
        Dim objNewRibbonBarTheme As SolidEdgeFramework.RibbonBarTheme = Nothing
        Dim nCustomeTheme_ComboIndex As Integer = 0
        Dim CreateTheme_Dialog As New CreateTheme
        Dim sNewThemeName As String = Nothing

        ' Show testDialog as a modal dialog and determine if DialogResult = OK.
        If CreateTheme_Dialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
            ' Read the contents of testDialog's TextBox.
            sNewThemeName = CreateTheme_Dialog.sThemeName
        Else
            Exit Sub  ' They decided not to create a new Theme.  Just exit.
        End If

        CreateTheme_Dialog.Dispose()


        Try
            ' Put this at the beginning.  
            objApp.Customization.BeginCustomization()

            nCustomeTheme_ComboIndex = Custom_Themes_ComboBox.SelectedIndex

            If (Custom_Themes_ComboBox.SelectedItem = "Solid Edge Default") Then
                objRibbonBarTheme = Nothing  ' Use Solid Edge Default as the base of the new theme.
            Else
                objRibbonBarTheme = GetTheme(objApp, Custom_Themes_ComboBox.SelectedItem)
            End If

            objNewRibbonBarTheme = objApp.Customization.RibbonBarThemes.Create(objRibbonBarTheme)
            objNewRibbonBarTheme.Name = sNewThemeName

            objApp.Customization.RibbonBarThemes.Commit()
            objApp.Customization.EndCustomization()

        Catch ex As Exception

        End Try


        LoadThemes(sNewThemeName)


        releaseObject(objRibbonBarTheme)
        releaseObject(objNewRibbonBarTheme)


    End Sub
    Private Sub LoadThemes(ByVal sSetTheme As String)
        Dim nCustomThemes As Integer = 0
        Dim objRibbonBarTheme As SolidEdgeFramework.RibbonBarTheme = Nothing

        Windows.Forms.Cursor.Current = Cursors.WaitCursor

        Try
            ' Put this at the beginning.  
            objApp.Customization.BeginCustomization()

            If (objApp.Customization.RibbonBarThemes IsNot Nothing) Then
                nCustomThemes = objApp.Customization.RibbonBarThemes.Count
            End If

            Custom_Themes_ComboBox.Items.Clear()  ' So we don't add the items over and over again.

            ' Add the Solid Edge Default
            Custom_Themes_ComboBox.Items.Add("Solid Edge Default")

            ' Add the Custom Themes to the Combo box.
            For i = 0 To nCustomThemes - 1
                objRibbonBarTheme = objApp.Customization.RibbonBarThemes.Item(i)

                Custom_Themes_ComboBox.Items.Add(objRibbonBarTheme.Name)

                releaseObject(objRibbonBarTheme)

            Next

            Custom_Themes_ComboBox.SelectedItem = sSetTheme ' Set the theme in the Combo box.

            If (nCustomThemes = 0) Then
                Custom_Themes_ComboBox.Enabled = False
                RemoveTheme_Btn.Enabled = False
            Else
                Custom_Themes_ComboBox.Enabled = True

                ' Can't remove the Solid Edge Default
                If (Custom_Themes_ComboBox.SelectedItem <> "Solid Edge Default") Then
                    RemoveTheme_Btn.Enabled = True
                End If

            End If

            objApp.Customization.EndCustomization()

        Catch ex As Exception

        End Try

        releaseObject(objRibbonBarTheme)
    End Sub

    Private Sub RemoveTheme_Btn_Click(sender As System.Object, e As System.EventArgs) Handles RemoveTheme_Btn.Click
        Dim nCustomeTheme_ComboIndex As Integer = 0
        Dim objRibbonBarTheme As SolidEdgeFramework.RibbonBarTheme = Nothing

        Try
            ' Put this at the beginning.  
            objApp.Customization.BeginCustomization()


            nCustomeTheme_ComboIndex = Custom_Themes_ComboBox.SelectedIndex

            If (nCustomeTheme_ComboIndex = -1) Then
                MsgBox("Please select a Custom Theme.")
            Else
                objRibbonBarTheme = GetTheme(objApp, Custom_Themes_ComboBox.SelectedItem)
                objApp.Customization.RibbonBarThemes.Remove(objRibbonBarTheme.Name)
            End If

            objApp.Customization.RibbonBarThemes.Commit()
            objApp.Customization.EndCustomization()

        Catch ex As Exception

        End Try

        LoadThemes("Solid Edge Default")
        releaseObject(objRibbonBarTheme)

    End Sub

    Private Sub Clear_Disable_Controls()
        RemoveTheme_Btn.Enabled = False

        Insert_Group_Btn.Enabled = False
        Remove_Group_Btn.Enabled = False

        Insert_Control_Btn.Enabled = False
        Remove_Control_Btn.Enabled = False

        GlobalControlSize_CBox.Items.Clear()
        GlobalControlSize_CBox.Enabled = False
        GlobalControlText_CBox.Items.Clear()
        GlobalControlText_CBox.Enabled = False
        RibbonBar_Env_ComboBox.Items.Clear()
        RibbonBar_Env_ComboBox.Enabled = False
        Insert_Btn.Enabled = False
        Remove_Btn.Enabled = False
        TreeView1.Nodes.Clear()
        TreeView1.Enabled = False

    End Sub

    Private Sub GlobalControlSize_CBox_SelectedIndexChanged_1(sender As System.Object, e As System.EventArgs) Handles GlobalControlSize_CBox.SelectedIndexChanged
        Dim objRibbonBarTheme As SolidEdgeFramework.RibbonBarTheme = Nothing

        Try
            ' Put this at the beginning.  
            If (bCustomize) Then
                objApp.Customization.BeginCustomization()
            End If


            objRibbonBarTheme = GetTheme(objApp, Custom_Themes_ComboBox.SelectedItem)

            If (GlobalControlSize_CBox.SelectedItem = sGlobalControlSize_Default) Then
                objRibbonBarTheme.GlobalControlSize = SolidEdgeFramework.RibbonBarControlSize.seRibbonBarControlSizeDefault
            Else
                If (GlobalControlSize_CBox.SelectedItem = sGlobalControlSize_Large) Then
                    objRibbonBarTheme.GlobalControlSize = SolidEdgeFramework.RibbonBarControlSize.seRibbonBarControlSizeLarge
                Else
                    objRibbonBarTheme.GlobalControlSize = SolidEdgeFramework.RibbonBarControlSize.seRibbonBarControlSizeSmall
                End If

            End If

            If (bCustomize) Then
                objApp.Customization.RibbonBarThemes.Commit()
                objApp.Customization.EndCustomization()
            End If

        Catch ex As Exception

        End Try
        releaseObject(objRibbonBarTheme)

    End Sub

    Private Sub GlobalControlText_CBox_SelectedIndexChanged_1(sender As System.Object, e As System.EventArgs) Handles GlobalControlText_CBox.SelectedIndexChanged
        Dim objRibbonBarTheme As SolidEdgeFramework.RibbonBarTheme = Nothing

        Try
            ' Put this at the beginning.  
            If (bCustomize) Then
                objApp.Customization.BeginCustomization()
            End If

            objRibbonBarTheme = GetTheme(objApp, Custom_Themes_ComboBox.SelectedItem)

            If (GlobalControlText_CBox.SelectedItem = sGlobalControlText_Default) Then
                objRibbonBarTheme.GlobalControlText = SolidEdgeFramework.RibbonBarControlText.seRibbonBarControlTextDefault
            Else
                If (GlobalControlText_CBox.SelectedItem = sGlobalControlText_NoText) Then
                    objRibbonBarTheme.GlobalControlText = SolidEdgeFramework.RibbonBarControlText.seRibbonBarControlTextOff
                Else
                    objRibbonBarTheme.GlobalControlText = SolidEdgeFramework.RibbonBarControlText.seRibbonBarControlTextOn
                End If

            End If

            If (bCustomize) Then
                objApp.Customization.RibbonBarThemes.Commit()
                objApp.Customization.EndCustomization()
            End If

        Catch ex As Exception

        End Try

        releaseObject(objRibbonBarTheme)
    End Sub

    Private Sub Insert_Btn_Click(sender As System.Object, e As System.EventArgs) Handles Insert_Btn.Click
        Dim Insert_Item_Dialog As New Insert_Item
        Dim sNewName As String = Nothing

        Dim sSelectedItem As String = Nothing
        Dim sCopy_MoveItem As String = Nothing

        Dim bInsertBefore As Boolean = True
        Dim objTab As SolidEdgeFramework.RibbonBarTab = Nothing
        Dim objTabs As SolidEdgeFramework.RibbonBarTabs = Nothing
        Dim objTabToCopy_Move As SolidEdgeFramework.RibbonBarTab = Nothing

        Dim nInsertMode As SolidEdgeFramework.RibbonBarInsertMode = SolidEdgeFramework.RibbonBarInsertMode.seRibbonBarInsertCreate

        Dim bUseIndex As Boolean = False

        Dim i As Integer = 0

        Dim objRibbonBar As SolidEdgeFramework.RibbonBar = Nothing
        Dim objRibbonBarTheme As SolidEdgeFramework.RibbonBarTheme = Nothing

        Dim objTreeNode As New TreeNode
        Dim bInsertTab As Boolean = False


        Windows.Forms.Cursor.Current = Cursors.WaitCursor


        Try

            Dim InfoArray() As String = Nothing
            InfoArray = Nothing

            ' Check to see if they have selected a Tab, Group, or Control
            objTreeNode = TreeView1.SelectedNode

            ' Put this at the beginning.  
            objApp.Customization.BeginCustomization()

            objRibbonBarTheme = GetTheme(objApp, Custom_Themes_ComboBox.SelectedItem)
            objRibbonBar = GetRibbonbar(objRibbonBarTheme)

            Insert_Item_Dialog.Browse.Visible = False

            ' Nothing selected can only Create
            If (objTreeNode Is Nothing) Then
                Insert_Item_Dialog.Create_RB.Enabled = True
                Insert_Item_Dialog.Copy_RB.Enabled = False
                Insert_Item_Dialog.Move_RB.Enabled = False
                Insert_Item_Dialog.InsertBefore_RB.Enabled = False
                Insert_Item_Dialog.InsertAfter_RB.Enabled = False
            Else
                InfoArray = Split(objTreeNode.FullPath, "\")
                If (InfoArray.Count = 1) Then
                    bInsertTab = True
                End If
            End If



            ' Either a tab is selected or there are no tabs.
            If (bInsertTab) Or (objTreeNode Is Nothing) Then

                ' There are tabs on the ribbon, add them to the list.
                If (InfoArray IsNot Nothing) Then
                    If (InfoArray.Count = 1) Then
                        ' Need to fill in the Listbox with the tabs just in case they do a Move or Copy.
                        For i = 0 To objRibbonBar.RibbonBarTabs.Count - 1
                            Insert_Item_Dialog.Commands_LB.Items.Add(objRibbonBar.RibbonBarTabs.Item(i).Name)
                        Next

                        ' Put the tab name on the text
                        Insert_Item_Dialog.InsertBefore_RB.Text = Insert_Item_Dialog.InsertBefore_RB.Text + " - " + InfoArray(0)
                        Insert_Item_Dialog.InsertAfter_RB.Text = Insert_Item_Dialog.InsertAfter_RB.Text + " - " + InfoArray(0)
                    End If
                End If

                ' Show testDialog as a modal dialog and determine if DialogResult = OK.
                If Insert_Item_Dialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then

                    ' Get the Insert mode (create, copy, move)
                    nInsertMode = Insert_Item_Dialog.nInsertMode

                    If (nInsertMode = SolidEdgeFramework.RibbonBarInsertMode.seRibbonBarInsertCopy) Or
                        (nInsertMode = SolidEdgeFramework.RibbonBarInsertMode.seRibbonBarInsertMove) Then

                        ' Get the Selected Tab
                        sCopy_MoveItem = Insert_Item_Dialog.Commands_LB.SelectedItem
                    End If


                    ' Read the contents of testDialog's TextBox.
                    sNewName = Insert_Item_Dialog.sNewName
                    bInsertBefore = Insert_Item_Dialog.InsertBefore_RB.Checked
                Else
                    Exit Sub  ' They decided not to create a new Theme.  Just exit.
                End If

                Insert_Item_Dialog.Dispose()


                Dim objNewTab As SolidEdgeFramework.RibbonBarTab = Nothing
                Dim objNewTab2 As SolidEdgeFramework.RibbonBarTab = Nothing

                ' There is no tab so add one.
                If (objTreeNode Is Nothing) Then
                    ' No tabs - add one.
                    objRibbonBar.RibbonBarTabs.Insert(sNewName, -1, nInsertMode)
                    GoTo No_Change
                End If



                objTab = GetRibbonTab(objRibbonBar, Nothing)
                If (objTab IsNot Nothing) Then
                    objTabs = objTab.Parent

                    ' If you want to insert it before the selected tab then you can just use that tab name.
                    If (bInsertBefore) Then
                        sSelectedItem = InfoArray(0)
                    Else
                        ' They want to insert it after the selected tab.  Find the next tab and use it.
                        For i = 0 To objTabs.Count - 1
                            If (InfoArray(0) = objTabs.Item(i).Name) Then
                                ' Check to make sure there is a tab after the one they selected.  If not then we will use another way to add it to the end.
                                If (i + 1 = objTabs.Count) Then
                                    ' They want to insert at the end.
                                    bUseIndex = True
                                Else
                                    sSelectedItem = objTabs.Item(i + 1).Name
                                End If
                                Exit For
                            End If
                        Next
                    End If


                    If (bUseIndex) Then
                        objNewTab = objTabs.Insert(sNewName, objTabs.Count, nInsertMode)
                    Else
                        If (nInsertMode = SolidEdgeFramework.RibbonBarInsertMode.seRibbonBarInsertCopy) Or
                        (nInsertMode = SolidEdgeFramework.RibbonBarInsertMode.seRibbonBarInsertMove) Then
                            For i = 0 To objTabs.Count - 1
                                If (sCopy_MoveItem = objTabs.Item(i).Name) Then
                                    objTabToCopy_Move = objTabs.Item(i)
                                    Exit For
                                End If
                            Next
                            Try
                                objNewTab = objTabs.Insert(objTabToCopy_Move.Name, sSelectedItem, nInsertMode)
                                objNewTab.Name = sNewName
                            Catch ex As Exception

                            End Try
                        Else
                            objNewTab = objTabs.Insert(sNewName, sSelectedItem, nInsertMode)
                        End If

                    End If

                    objNewTab.Visible = True

                End If
            End If


No_Change:

            objApp.Customization.RibbonBarThemes.Commit()
            objApp.Customization.EndCustomization()


            Insert_Btn.Enabled = False
            Remove_Btn.Enabled = False
            Insert_Group_Btn.Enabled = False
            Remove_Group_Btn.Enabled = False
            Insert_Control_Btn.Enabled = False
            Remove_Control_Btn.Enabled = False

            ' Unselect the item and disable the controls.
            TreeView1.SelectedNode = Nothing
            Update_Tree_Based_on_Selected_Env()

        Catch ex As Exception

        End Try

        releaseObject(objTab)
        releaseObject(objTabs)
        releaseObject(objTabToCopy_Move)


    End Sub
    Private Sub Update_Tree_Based_on_Selected_Env()
        Dim i As Integer = 0
        Dim objRibbonBarTheme As SolidEdgeFramework.RibbonBarTheme = Nothing

        Dim objRibbonBar As SolidEdgeFramework.RibbonBar = Nothing
        Dim aItems As New ArrayList
        aItems.Clear()


        Windows.Forms.Cursor.Current = Cursors.WaitCursor

        Try
            objRibbonBarTheme = GetTheme(objApp, Custom_Themes_ComboBox.SelectedItem)


            For i = 0 To objRibbonBarTheme.RibbonBars.Count - 1
                If (RibbonBar_Env_ComboBox.SelectedItem = objRibbonBarTheme.RibbonBars.Item(i).Environment) Then
                    objRibbonBar = objRibbonBarTheme.RibbonBars.Item(i)
                    Exit For
                End If
            Next

           
            TreeView1.BeginUpdate()
            TreeView1.Nodes.Clear()
           
            Get_Tabs_Groups_Controls(aItems)

            If (aItems.Count > 0) Then
                Add_Tree_Node(Nothing, Nothing, aItems)
            Else
                'If there are no items then enable the Insert tab button.
                Insert_Btn.Enabled = True
            End If
   


        Catch ex As Exception

        End Try

        TreeView1.EndUpdate()

        ' They have selected an environment so enable the tree ctrl
        TreeView1.Enabled = True

        releaseObject(objRibbonBarTheme)
    End Sub
    Private Function RibbonItemVisible(ByRef sNodeInfo As String)
        Dim InfoArray() As String = Nothing
        RibbonItemVisible = False

        Try
            If (sNodeInfo IsNot Nothing) Then
                InfoArray = Split(sNodeInfo, ",")
                sNodeInfo = InfoArray(0)
                RibbonItemVisible = Convert.ToBoolean(InfoArray(1))
            Else
                RibbonItemVisible = False
            End If

        Catch ex As Exception

        End Try


    End Function
    Private Sub Add_Tree_Node(ByVal ParentNode As TreeNode, ByVal fromNode As TreeNode, ByVal aNewNodeNames As ArrayList)

        Dim InfoArray() As String = Nothing
        Dim sNewParent As String = Nothing
        Dim nLoc As Integer = 0

        Try

            While (aNewNodeNames.Count > 0)
                Dim bVisible As Boolean = False

                ' First get whether it is visible or not.
                bVisible = RibbonItemVisible(aNewNodeNames(0))

                InfoArray = Split(aNewNodeNames(0), "\")
                If (InfoArray.Count > 1) Then
                    nLoc = Microsoft.VisualBasic.InStrRev(aNewNodeNames(0), "\")
                    sNewParent = Microsoft.VisualBasic.Left(aNewNodeNames(0), nLoc - 1)
                End If

                If (InfoArray.Count = 1) Then
                    If (InfoArray(0) <> "") Then
                        ParentNode = New TreeNode(InfoArray(0))
                        TreeView1.Nodes.Add(ParentNode)
                        ParentNode.Checked = bVisible
                    End If

                    aNewNodeNames.RemoveAt(0)

                    Add_Tree_Node(ParentNode, ParentNode, aNewNodeNames)
                Else
                    If (fromNode.FullPath = sNewParent) Then
                        If (InfoArray(InfoArray.Count - 1) <> "") Then
                            ParentNode = fromNode.Nodes.Add(InfoArray(InfoArray.Count - 1))
                            ParentNode.Checked = bVisible
                        End If
                        aNewNodeNames.RemoveAt(0)

                        Add_Tree_Node(ParentNode, fromNode, aNewNodeNames)
                    Else
                        If (ParentNode.FullPath = sNewParent) Then
                            fromNode = ParentNode
                            If (InfoArray(InfoArray.Count - 1) <> "") Then
                                ParentNode = ParentNode.Nodes.Add(InfoArray(InfoArray.Count - 1))
                                ParentNode.Checked = bVisible
                            End If

                            aNewNodeNames.RemoveAt(0)

                            Add_Tree_Node(ParentNode, fromNode, aNewNodeNames)

                        Else
                            aNewNodeNames(0) = aNewNodeNames(0) + "," + bVisible.ToString
                            Exit While
                        End If

                    End If
                End If

            End While

        Catch ex As Exception

        End Try

    End Sub


    Private Sub GetControlNames(ByVal objControls As SolidEdgeFramework.RibbonBarControls, ByVal sParent As String, ByRef aControls As ArrayList, ByRef aSort As ArrayList)
        Dim i As Integer = 0
        Dim nLoc As Integer = 0

        Try
            For i = 0 To objControls.Count - 1
                If (aControls.Count >= 1) Then
                    'nLoc = aControls.BinarySearch(objControls.Item(i).Name)
                    'If (nLoc < 0) Then
                    If (sParent Is Nothing) Then
                        If Not (AlreadyInArray(objControls.Item(i).Name, aSort)) Then
                            aControls.Add(objControls.Item(i).Name + "," + objControls.Item(i).Visible.ToString)
                        End If

                    Else
                        If Not (AlreadyInArray(sParent + "\" + objControls.Item(i).Name, aSort)) Then
                            aControls.Add(sParent + "\" + objControls.Item(i).Name + "," + objControls.Item(i).Visible.ToString)
                        End If

                    End If

                Else
                    If Not (AlreadyInArray(objControls.Item(i).Name, aSort)) Then
                        aControls.Add(objControls.Item(i).Name + "," + objControls.Item(i).Visible.ToString)
                    End If

                End If



                If (objControls.Item(i).RibbonBarControls IsNot Nothing) Then
                    If (sParent Is Nothing) Then
                        GetControlNames(objControls.Item(i).RibbonBarControls, objControls.Item(i).Name, aControls, aSort)
                    Else
                        GetControlNames(objControls.Item(i).RibbonBarControls, sParent + "\" + objControls.Item(i).Name, aControls, aSort)
                    End If

                End If
            Next

        Catch ex As Exception

        End Try
    End Sub
    Private Function AlreadyInArray(ByVal sName As String, ByRef aSort As ArrayList)
        Dim nLoc As Integer = 0


        If aSort.Count = 0 Then
            aSort.Add(sName)
            AlreadyInArray = False
        Else
            aSort.Sort()
            nLoc = aSort.BinarySearch(sName)
            If (nLoc < 0) Then
                aSort.Add(sName)
                AlreadyInArray = False
            Else
                AlreadyInArray = True
            End If
        End If


    End Function
    Private Sub Get_Tabs_Groups_Controls(ByRef aItems As ArrayList)
        Dim i As Integer = 0
        Dim objRibbonBar As SolidEdgeFramework.RibbonBar = Nothing
        Dim objRibbonBarTheme As SolidEdgeFramework.RibbonBarTheme = Nothing
        Dim aSort As New ArrayList
        aSort.Clear()


        objRibbonBarTheme = GetTheme(objApp, Custom_Themes_ComboBox.SelectedItem)

        objRibbonBar = GetRibbonbar(objRibbonBarTheme)

        For i = 0 To objRibbonBar.RibbonBarTabs.Count - 1

            If Not (AlreadyInArray(objRibbonBar.RibbonBarTabs.Item(i).Name, aSort)) Then
                aItems.Add(objRibbonBar.RibbonBarTabs.Item(i).Name + "," + objRibbonBar.RibbonBarTabs.Item(i).Visible.ToString)
            End If


            For j = 0 To objRibbonBar.RibbonBarTabs.Item(i).RibbonBarGroups.Count - 1

                If Not (AlreadyInArray(objRibbonBar.RibbonBarTabs.Item(i).Name + "\" + objRibbonBar.RibbonBarTabs.Item(i).RibbonBarGroups.Item(j).Name, aSort)) Then
                    aItems.Add(objRibbonBar.RibbonBarTabs.Item(i).Name + "\" + objRibbonBar.RibbonBarTabs.Item(i).RibbonBarGroups.Item(j).Name + "," + objRibbonBar.RibbonBarTabs.Item(i).RibbonBarGroups.Item(j).Visible.ToString)
                End If


                ' Test new function
                GetControlNames(objRibbonBar.RibbonBarTabs.Item(i).RibbonBarGroups.Item(j).RibbonBarControls, objRibbonBar.RibbonBarTabs.Item(i).Name + "\" + objRibbonBar.RibbonBarTabs.Item(i).RibbonBarGroups.Item(j).Name, aItems, aSort)

            Next

        Next
        releaseObject(objRibbonBar)
        releaseObject(objRibbonBarTheme)

    End Sub


    Private Sub Remove_Group_Btn_Click(sender As System.Object, e As System.EventArgs) Handles Remove_Group_Btn.Click
        Dim objRibbonBarTheme As SolidEdgeFramework.RibbonBarTheme = Nothing
        Dim objRibbonBar As SolidEdgeFramework.RibbonBar = Nothing
        Dim objGroup As SolidEdgeFramework.RibbonBarGroup = Nothing
        Dim objGroups As SolidEdgeFramework.RibbonBarGroups = Nothing
        '

        Try
            ' Put this at the beginning.  
            objApp.Customization.BeginCustomization()

            objRibbonBarTheme = GetTheme(objApp, Custom_Themes_ComboBox.SelectedItem)

            objRibbonBar = GetRibbonbar(objRibbonBarTheme)


            'Remove the entry from the tree ctrl
            Dim objTreeNode As New TreeNode

            objTreeNode = TreeView1.SelectedNode

            objGroup = GetRibbonGroup(objRibbonBar, Nothing)

            objGroups = objGroup.Parent
            objGroups.Remove(objGroup)

            objTreeNode.Remove()
            objTreeNode = Nothing

            objApp.Customization.RibbonBarThemes.Commit()
            objApp.Customization.EndCustomization()


            Insert_Btn.Enabled = False
            Remove_Btn.Enabled = False
            Insert_Group_Btn.Enabled = False
            Remove_Group_Btn.Enabled = False
            Insert_Control_Btn.Enabled = False
            Remove_Control_Btn.Enabled = False



        Catch ex As Exception

        End Try


        releaseObject(objRibbonBar)
        releaseObject(objRibbonBarTheme)
        releaseObject(objGroup)
        releaseObject(objGroups)

        ' Unselect the node.
        TreeView1.SelectedNode = Nothing


    End Sub

    Private Sub Remove_Control_Btn_Click(sender As System.Object, e As System.EventArgs) Handles Remove_Control_Btn.Click
        Dim objRibbonBarTheme As SolidEdgeFramework.RibbonBarTheme = Nothing
        Dim objRibbonBar As SolidEdgeFramework.RibbonBar = Nothing
        Dim objControl As SolidEdgeFramework.RibbonBarControl = Nothing
        Dim objControls As SolidEdgeFramework.RibbonBarControls = Nothing


        Try
            ' Put this at the beginning.  
            objApp.Customization.BeginCustomization()

            objRibbonBarTheme = GetTheme(objApp, Custom_Themes_ComboBox.SelectedItem)

            objRibbonBar = GetRibbonbar(objRibbonBarTheme)


            'Remove the entry from the tree ctrl
            Dim objTreeNode As New TreeNode

            objTreeNode = TreeView1.SelectedNode

            objControl = GetRibbonControl(objRibbonBar, Nothing)

            objControls = objControl.Parent
            objControls.Remove(objControl)

            objTreeNode.Remove()
            objTreeNode = Nothing

            objApp.Customization.RibbonBarThemes.Commit()
            objApp.Customization.EndCustomization()


            Insert_Btn.Enabled = False
            Remove_Btn.Enabled = False
            Insert_Group_Btn.Enabled = False
            Remove_Group_Btn.Enabled = False
            Insert_Control_Btn.Enabled = False
            Remove_Control_Btn.Enabled = False

        Catch ex As Exception

        End Try

        releaseObject(objRibbonBar)
        releaseObject(objRibbonBarTheme)
        releaseObject(objControl)
        releaseObject(objControls)


        ' Unselect the node.
        TreeView1.SelectedNode = Nothing
    End Sub

    Private Sub Insert_Group_Btn_Click(sender As System.Object, e As System.EventArgs) Handles Insert_Group_Btn.Click
        Dim Insert_Item_Dialog As New Insert_Item
        Dim sNewName As String = Nothing
        Dim objNewGroup As SolidEdgeFramework.RibbonBarGroup = Nothing

        Dim sSelectedItem As String = Nothing
        Dim sCopy_MoveItem As String = Nothing

        Dim bInsertBefore As Boolean = True
        Dim objTab As SolidEdgeFramework.RibbonBarTab = Nothing
        Dim objGroup As SolidEdgeFramework.RibbonBarGroup = Nothing
        Dim objGroups As SolidEdgeFramework.RibbonBarGroups = Nothing
        Dim objSelectedGroup As SolidEdgeFramework.RibbonBarGroup = Nothing
        Dim objTabToCopy_Move As SolidEdgeFramework.RibbonBarTab = Nothing
        Dim nInsertMode As SolidEdgeFramework.RibbonBarInsertMode = SolidEdgeFramework.RibbonBarInsertMode.seRibbonBarInsertCreate
        Dim bUseIndex As Boolean = False
        Dim i As Integer = 0
        Dim objRibbonBar As SolidEdgeFramework.RibbonBar = Nothing
        Dim objRibbonBarTheme As SolidEdgeFramework.RibbonBarTheme = Nothing
        Dim objTreeNode As New TreeNode
        Dim bInsertGroupinSelectedTab As Boolean = False


        Windows.Forms.Cursor.Current = Cursors.WaitCursor


        Try
            Dim InfoArray() As String = Nothing
            InfoArray = Nothing

            ' Check to see if they have selected a Tab, Group, or Control
            objTreeNode = TreeView1.SelectedNode

            ' Put this at the beginning.  
            objApp.Customization.BeginCustomization()

            objRibbonBarTheme = GetTheme(objApp, Custom_Themes_ComboBox.SelectedItem)
            objRibbonBar = GetRibbonbar(objRibbonBarTheme)

            Insert_Item_Dialog.Browse.Visible = False

            ' Nothing selected can only Create
            If (objTreeNode Is Nothing) Then
                Insert_Item_Dialog.Create_RB.Enabled = True
                Insert_Item_Dialog.Copy_RB.Enabled = False
                Insert_Item_Dialog.Move_RB.Enabled = False
                Insert_Item_Dialog.InsertBefore_RB.Enabled = False
                Insert_Item_Dialog.InsertAfter_RB.Enabled = False
            Else
                InfoArray = Split(objTreeNode.FullPath, "\")
                If (InfoArray.Count = 1) Then
                    bInsertGroupinSelectedTab = True
                End If
            End If


            Dim bNoGroups As Boolean = False

            ' Put the tab name on the text
            ' If there is only one entry then a tab is selected an we only add the group.
            If (InfoArray.Count = 1) Then
                Insert_Item_Dialog.InsertBefore_RB.Enabled = False
                Insert_Item_Dialog.InsertAfter_RB.Enabled = False
                Insert_Item_Dialog.Copy_RB.Enabled = False
                Insert_Item_Dialog.Move_RB.Enabled = False
                bNoGroups = True
            Else
                Insert_Item_Dialog.InsertBefore_RB.Text = "Insert Group Before - " + InfoArray(1)
                Insert_Item_Dialog.InsertAfter_RB.Text = "Insert Group After - " + InfoArray(1)
                Insert_Item_Dialog.Select_LB.Text = "Select the group to copy / move:"
            End If
            Insert_Item_Dialog.Label1.Text = "New Group Name:"


            objRibbonBarTheme = GetTheme(objApp, Custom_Themes_ComboBox.SelectedItem)
            objRibbonBar = GetRibbonbar(objRibbonBarTheme)

            If (bNoGroups = False) Then
                objGroup = GetRibbonGroup(objRibbonBar, Nothing)
                objGroups = objGroup.Parent

                ' Need to fill in the Listbox with the tabs just in case they do a Move or Copy.
                For i = 0 To objGroups.Count - 1
                    Insert_Item_Dialog.Commands_LB.Items.Add(objGroups.Item(i).Name)
                Next
            Else

                If (bInsertGroupinSelectedTab = False) Then
                    objGroup = GetRibbonGroup(objRibbonBar, Nothing)
                    objGroups = objGroup.Parent()
                Else
                    objTab = GetRibbonTab(objRibbonBar, Nothing)
                    objGroups = objTab.RibbonBarGroups()

                End If
            End If


            ' Show testDialog as a modal dialog and determine if DialogResult = OK.
            If Insert_Item_Dialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then

                ' Get the Insert mode (create, copy, move)
                nInsertMode = Insert_Item_Dialog.nInsertMode


                ' Read the contents of testDialog's TextBox.
                sNewName = Insert_Item_Dialog.sNewName
                bInsertBefore = Insert_Item_Dialog.InsertBefore_RB.Checked
                If (nInsertMode <> SolidEdgeFramework.RibbonBarInsertMode.seRibbonBarInsertCreate) Then
                    sSelectedItem = Insert_Item_Dialog.Commands_LB.SelectedItem.ToString

                    ' Need to get the selected Group
                    For i = 0 To objGroups.Count - 1
                        If (objGroups.Item(i).Name = sSelectedItem) Then
                            objSelectedGroup = objGroups.Item(i)
                            Exit For
                        End If
                    Next

                End If

            Else
                Exit Sub  ' They decided not to create a new Theme.  Just exit.
            End If

            Insert_Item_Dialog.Dispose()



            ' No groups on tab, just add it.  Or just doing a Create
            If (nInsertMode = SolidEdgeFramework.RibbonBarInsertMode.seRibbonBarInsertCreate) Or (bNoGroups) Or (bInsertGroupinSelectedTab) Then

                If (objGroups.Count > 0) Then
                    objGroups.Insert(sNewName, 0, nInsertMode)
                Else
                    objGroups.Insert(sNewName, -1, nInsertMode)
                End If

                GoTo End_Add_Group
            End If


            If (bInsertBefore) Then
                sSelectedItem = objSelectedGroup.Name
            Else
                ' They want to insert it after the selected Group.  Find the next Group and use it.
                For i = 0 To objGroups.Count - 1
                    If (objGroup.Name = objGroups.Item(i).Name) Then
                        ' Check to make sure there is a tab after the one they selected.  If not then we will use another way to add it to the end.
                        If (i + 1 = objGroups.Count) Then
                            ' They want to insert at the end.
                            bUseIndex = True
                        Else
                            objGroup = objGroups.Item(i + 1)
                        End If
                        Exit For
                    End If
                Next
            End If


            If (bUseIndex) Then
                objNewGroup = objGroups.Insert(objSelectedGroup, objGroups.Count, nInsertMode)
            Else
                objNewGroup = objGroups.Insert(objSelectedGroup, objGroup, nInsertMode)
            End If

            objNewGroup.Name = sNewName

        Catch ex As Exception

        End Try

End_Add_Group:

        Try
            If (objNewGroup IsNot Nothing) Then
                objNewGroup.Visible = True
            End If


            objApp.Customization.RibbonBarThemes.Commit()
            objApp.Customization.EndCustomization()


            Insert_Btn.Enabled = False
            Remove_Btn.Enabled = False
            Insert_Group_Btn.Enabled = False
            Remove_Group_Btn.Enabled = False
            Insert_Control_Btn.Enabled = False
            Remove_Control_Btn.Enabled = False

            ' Unselect the item and disable the controls.
            TreeView1.SelectedNode = Nothing
            Update_Tree_Based_on_Selected_Env()


        Catch ex As Exception

        End Try

        releaseObject(objNewGroup)
        releaseObject(objGroup)
        releaseObject(objGroups)
        releaseObject(objNewGroup)
        releaseObject(objSelectedGroup)
        releaseObject(objTab)

    End Sub

    Private Sub Insert_Control_Btn_Click(sender As System.Object, e As System.EventArgs) Handles Insert_Control_Btn.Click

        Dim Insert_Item_Dialog As New Insert_Item
        Dim sNewName As String = Nothing

        Dim sSelectedItem As String = Nothing
        Dim sCopy_MoveItem As String = Nothing

        Dim bInsertBefore As Boolean = True
        Dim objGroup As SolidEdgeFramework.RibbonBarGroup = Nothing
        Dim objControl As SolidEdgeFramework.RibbonBarControl = Nothing
        Dim objControls As SolidEdgeFramework.RibbonBarControls = Nothing
        Dim objSelectedControl As SolidEdgeFramework.RibbonBarControl = Nothing
        Dim objNewControl As SolidEdgeFramework.RibbonBarControl = Nothing


        Dim nInsertMode As SolidEdgeFramework.RibbonBarInsertMode = SolidEdgeFramework.RibbonBarInsertMode.seRibbonBarInsertCreate

        Dim bUseIndex As Boolean = False

        Dim i As Integer = 0

        Dim objRibbonBar As SolidEdgeFramework.RibbonBar = Nothing
        Dim objRibbonBarTheme As SolidEdgeFramework.RibbonBarTheme = Nothing

        Dim objTreeNode As New TreeNode
        Dim bInsertControlInSelectedGroup As Boolean = False


        Dim InfoArray() As String = Nothing
        InfoArray = Nothing

        Windows.Forms.Cursor.Current = Cursors.WaitCursor

        Try

            ' Check to see if they have selected a Tab, Group, or Control
            objTreeNode = TreeView1.SelectedNode

            ' Put this at the beginning before you do any customization
            objApp.Customization.BeginCustomization()

            objRibbonBarTheme = GetTheme(objApp, Custom_Themes_ComboBox.SelectedItem)
            objRibbonBar = GetRibbonbar(objRibbonBarTheme)

            ' Nothing selected can only Create
            If (objTreeNode Is Nothing) Then
                Insert_Item_Dialog.Create_RB.Enabled = True
                Insert_Item_Dialog.Copy_RB.Enabled = False
                Insert_Item_Dialog.Move_RB.Enabled = False
                Insert_Item_Dialog.InsertBefore_RB.Enabled = False
                Insert_Item_Dialog.InsertAfter_RB.Enabled = False
            Else
                InfoArray = Split(objTreeNode.FullPath, "\")
                If (InfoArray.Count = 2) Then
                    bInsertControlInSelectedGroup = True
                End If
            End If

            If (bInsertControlInSelectedGroup) Then
                Insert_Item_Dialog.InsertBefore_RB.Text = "Insert Control In - " + InfoArray(InfoArray.Count - 1)
                Insert_Item_Dialog.InsertBefore_RB.Checked = True
                Insert_Item_Dialog.InsertAfter_RB.Visible = False
                Insert_Item_Dialog.Select_LB.Text = "Select the control to copy / move:"
            Else
                Insert_Item_Dialog.InsertBefore_RB.Text = "Insert Control Before - " + InfoArray(InfoArray.Count - 1)
                Insert_Item_Dialog.InsertAfter_RB.Text = "Insert Control After - " + InfoArray(InfoArray.Count - 1)
                Insert_Item_Dialog.Select_LB.Text = "Select the control to copy / move:"

            End If

            ' Add a executable
            Insert_Item_Dialog.Label1.Text = "Select an executable"
            Insert_Item_Dialog.Browse.Visible = True
            Insert_Item_Dialog.Label1.Visible = True
            Insert_Item_Dialog.Name_Edit.Visible = True
            Insert_Item_Dialog.Commands_LB.Visible = False

            ' Add commands to the list box.
            Dim aItems As New ArrayList
            aItems.Clear()

            Dim nLoc As Integer = 0
            Dim aControls As New ArrayList
            aControls.Clear()

            Get_Tabs_Groups_Controls(aItems)



            ' Put this at the beginning before you do any customization 
            objApp.Customization.BeginCustomization()

            objRibbonBarTheme = GetTheme(objApp, Custom_Themes_ComboBox.SelectedItem)

            objRibbonBar = GetRibbonbar(objRibbonBarTheme)

            If (bInsertControlInSelectedGroup = False) Then  ' they have selected a Control otherwise they have selected a group.
                objControl = GetRibbonControl(objRibbonBar, Nothing)
                objControls = objControl.Parent
            Else
                objGroup = GetRibbonGroup(objRibbonBar, Nothing)
            End If

            Dim bVisible As Boolean = False

            For i = 0 To aItems.Count - 1

                InfoArray = Nothing

                ' This will remove the visible flag I added to the end of the string.
                bVisible = RibbonItemVisible(aItems(i))

                InfoArray = Split(aItems(i), "\")

                If (InfoArray.Count > 2) Then
                    ' Check to see if the control name / length is greater than 0  (the line breaks in the UI are -------")
                    If (InfoArray(InfoArray.Count - 1).Length > 0) Then
                        If (aControls.Count = 0) Then
                            aControls.Add(InfoArray(InfoArray.Count - 1))

                        Else
                            nLoc = aControls.BinarySearch(InfoArray(InfoArray.Count - 1))
                            If (nLoc < 0) Then
                                aControls.Add(InfoArray(InfoArray.Count - 1))
                                aControls.Sort()
                            End If
                        End If
                    End If

                End If

            Next

            ' Add the controls to the List box so they can choose which ones to copy.
            For i = 0 To aControls.Count - 1
                'Insert_Item_Dialog.Commands_LB.Items.Add(aUnsortedControls(i))
                Insert_Item_Dialog.Commands_LB.Items.Add(aControls(i))
            Next


            ' Show testDialog as a modal dialog and determine if DialogResult = OK.
            If Insert_Item_Dialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then

                ' Read the contents of testDialog's TextBox.
                bInsertBefore = Insert_Item_Dialog.InsertBefore_RB.Checked

                ' Get the Insert mode (create, copy, move)
                nInsertMode = Insert_Item_Dialog.nInsertMode

                If (nInsertMode = SolidEdgeFramework.RibbonBarInsertMode.seRibbonBarInsertCreate) Then
                    sNewName = Insert_Item_Dialog.sNewName
                Else
                    sSelectedItem = Insert_Item_Dialog.Commands_LB.SelectedItem.ToString
                End If

            Else
                Exit Sub  ' They decided not to create a new Theme.  Just exit.
            End If

            Insert_Item_Dialog.Dispose()

            For i = 0 To aItems.Count - 1
                If (aItems(i).ToString.EndsWith("\" + sSelectedItem)) Then
                    sSelectedItem = aItems(i)
                    Exit For
                End If
            Next



            objSelectedControl = GetRibbonControl(objRibbonBar, sSelectedItem)

            ' This is how to add a exe or dll as a control
            Dim macroArray(1) As Object
            If (sNewName IsNot Nothing) Then
                macroArray(0) = sNewName  ' Example:  "C:\Windows\Notepad.exe"
                'macroArray(1) = "new.txt"  ' You can pass an argument 
            End If


            Dim nInsertLoc As Integer = 0

            If (bInsertControlInSelectedGroup = False) Then
                If (sNewName Is Nothing) Then
                    objNewControl = objControls.Insert(objSelectedControl, objControl, nInsertMode)
                Else
                    objNewControl = objControls.Insert(macroArray, objControl, nInsertMode)
                End If

            Else
                objControls = objGroup.RibbonBarControls()

                If (sNewName Is Nothing) Then
                    objNewControl = objControls.Insert(objSelectedControl, nInsertLoc, nInsertMode)
                Else
                    objNewControl = objControls.Insert(macroArray, nInsertLoc, nInsertMode)
                End If

            End If

            objNewControl.Visible = True

            objApp.Customization.RibbonBarThemes.Commit()
            objApp.Customization.EndCustomization()

            Insert_Btn.Enabled = False
            Remove_Btn.Enabled = False
            Insert_Group_Btn.Enabled = False
            Remove_Group_Btn.Enabled = False
            Insert_Control_Btn.Enabled = False
            Remove_Control_Btn.Enabled = False


            Update_Tree_Based_on_Selected_Env()

        Catch ex As Exception

        End Try

        ' Unselect the item and disable the controls.
        TreeView1.SelectedNode = Nothing
        releaseObject(objGroup)
        releaseObject(objControl)
        releaseObject(objControls)
        releaseObject(objSelectedControl)
        releaseObject(objNewControl)
        releaseObject(objRibbonBar)
        releaseObject(objRibbonBarTheme)
    End Sub

    Private Sub Form1_FormClosed(sender As System.Object, e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed

        ' This is done to allow calls into SE to be rerun if SE wasn't able to the first time.
        MyMessageFilter.MessageFilter.Revoke()

        '  Clear up objects
        releaseObject(objApp)
        CleanupObjects()

    End Sub

    Public Sub releaseObject(ByRef obj As Object)

        Try
            If (obj IsNot Nothing) Then
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
                obj = Nothing
            End If
        Catch ex As Exception
            obj = Nothing
        Finally

        End Try
    End Sub
    Public Sub CleanupObjects()
        Try
            GC.Collect(GC.MaxGeneration())
            GC.WaitForPendingFinalizers()
            GC.Collect(GC.MaxGeneration())
        Catch ex As Exception

        End Try
    End Sub

    Private Sub ControlSize_CBox_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ControlSize_CBox.SelectedIndexChanged
        Dim objRibbonBarTheme As SolidEdgeFramework.RibbonBarTheme = Nothing
        Dim objRibbonBar As SolidEdgeFramework.RibbonBar = Nothing
        Dim objControl As SolidEdgeFramework.RibbonBarControl = Nothing

        If (bCustomize) Then
            ' Put this at the beginning.  
            objApp.Customization.BeginCustomization()
        End If

        objRibbonBarTheme = GetTheme(objApp, Custom_Themes_ComboBox.SelectedItem)
        objRibbonBar = GetRibbonbar(objRibbonBarTheme)

        objControl = GetRibbonControl(objRibbonBar, Nothing)

        If (ControlSize_CBox.SelectedItem = sGlobalControlSize_Default) Then
            objControl.Size = SolidEdgeFramework.RibbonBarControlSize.seRibbonBarControlSizeDefault
        Else
            If (ControlSize_CBox.SelectedItem = sGlobalControlSize_Large) Then
                objControl.Size = SolidEdgeFramework.RibbonBarControlSize.seRibbonBarControlSizeLarge
            Else
                objControl.Size = SolidEdgeFramework.RibbonBarControlSize.seRibbonBarControlSizeSmall
            End If

        End If

        If (bCustomize) Then
            objApp.Customization.RibbonBarThemes.Commit()
            objApp.Customization.EndCustomization()
        End If

        releaseObject(objRibbonBarTheme)
        releaseObject(objRibbonBar)
        releaseObject(objControl)

    End Sub

    Private Sub ControlText_CBox_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ControlText_CBox.SelectedIndexChanged
        Dim objRibbonBarTheme As SolidEdgeFramework.RibbonBarTheme = Nothing
        Dim objRibbonBar As SolidEdgeFramework.RibbonBar = Nothing
        Dim objControl As SolidEdgeFramework.RibbonBarControl = Nothing


        If (bCustomize) Then
            ' Put this at the beginning.  
            objApp.Customization.BeginCustomization()
        End If


        objRibbonBarTheme = GetTheme(objApp, Custom_Themes_ComboBox.SelectedItem)
        objRibbonBar = GetRibbonbar(objRibbonBarTheme)

        objControl = GetRibbonControl(objRibbonBar, Nothing)


        If (ControlText_CBox.SelectedItem = sGlobalControlText_Default) Then
            objControl.Text = SolidEdgeFramework.RibbonBarControlText.seRibbonBarControlTextDefault
        Else
            If (ControlText_CBox.SelectedItem = sGlobalControlText_NoText) Then
                objControl.Text = SolidEdgeFramework.RibbonBarControlText.seRibbonBarControlTextOff
            Else
                objControl.Text = SolidEdgeFramework.RibbonBarControlText.seRibbonBarControlTextOn
            End If

        End If


        If (bCustomize) Then
            objApp.Customization.RibbonBarThemes.Commit()
            objApp.Customization.EndCustomization()
        End If

        releaseObject(objRibbonBarTheme)
        releaseObject(objRibbonBar)
        releaseObject(objControl)
    End Sub

    Private Sub StartCommand_BTN_Click(sender As System.Object, e As System.EventArgs) Handles StartCommand_BTN.Click
        Dim sCommandId As String = Nothing
        sCommandId = ControlID_TB.Text.Trim()
        Try
            If (sCommandId IsNot Nothing) Then
                objApp.StartCommand(sCommandId)
            End If
        Catch ex As Exception
            MsgBox("Unable to run that commmand")
        End Try
    End Sub
End Class
